/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InvoicePanel.java
 *
 * Created on Feb 22, 2011, 1:16:57 PM
 */

package POSTApp;

import Product.Product;
import Transaction.TransactionItem;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 * GUI panel for keeping a list of items being bought.
 * @author Rebecca Bettencourt
 */
public class InvoicePanel extends javax.swing.JPanel {

    /** Creates new form InvoicePanel */
    public InvoicePanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        invoiceLabel = new javax.swing.JLabel();
        totalAmount = new javax.swing.JLabel();
        totalLabel = new javax.swing.JLabel();
        invoicePane = new javax.swing.JScrollPane();
        invoiceTable = new javax.swing.JTable();

        invoiceLabel.setText("Invoice:");

        totalAmount.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        totalAmount.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        totalAmount.setText("$0.00");

        totalLabel.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        totalLabel.setText("TOTAL");

        invoicePane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        invoicePane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        invoiceTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "UPC", "Description", "Quantity", "Item Price", "Total Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Float.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        invoicePane.setViewportView(invoiceTable);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(invoiceLabel)
                .addContainerGap(445, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(359, Short.MAX_VALUE)
                .add(totalLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(totalAmount, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(invoicePane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(invoiceLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(invoicePane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(totalAmount)
                    .add(totalLabel)))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel invoiceLabel;
    private javax.swing.JScrollPane invoicePane;
    private javax.swing.JTable invoiceTable;
    private javax.swing.JLabel totalAmount;
    private javax.swing.JLabel totalLabel;
    // End of variables declaration//GEN-END:variables

    public void clear() {
        DefaultTableModel dtm = (DefaultTableModel)invoiceTable.getModel();
        while (dtm.getRowCount() > 0) dtm.removeRow(0);
    }

    public void addItem(int upc, String description, int qty, float price) {
        DefaultTableModel dtm = (DefaultTableModel)invoiceTable.getModel();
        dtm.addRow(new Object[]{
            upc,
            description,
            qty,
            price,
            price * qty
        });
    }

    public void addItem(Product p, int qty) {
        addItem(p.getUPC(), p.getDescription(), qty, p.getPrice());
    }

    public List<TransactionItem> getItems() {
        List<TransactionItem> items = new ArrayList<TransactionItem>();
        DefaultTableModel dtm = (DefaultTableModel)invoiceTable.getModel();
        for (int i = 0; i < dtm.getRowCount(); i++) {
            int upc = ((Number)dtm.getValueAt(i, 0)).intValue();
            int qty = ((Number)dtm.getValueAt(i, 2)).intValue();
            items.add(new TransactionItem(upc, qty));
        }
        return items;
    }
}
